<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <title>Title</title>
</head>

<body>
    <script>
        //（1）首参是正则，2参是字符串
        // $1,$2表示分组捕获的文本，$&匹配到的子串文本，
        // $`匹配到的子串的左边的文本,$'匹配到的子串的右边的文本，$$美元符号。
        var str = '2,3,5';
        var result1 = str.replace(/(\d+),(\d+),(\d+)/, '$3=$1+$2');
        console.log(result1); //字符串：5=2+3
        var result2 = str.replace(/(\d+)/g, '$&$&$&');
        console.log(result2); //222,333,555
        var str3 = '2+3=5';
        var result3 = str3.replace(/=/, "$&$`$&$'$&");
        //由于此处里面有一个单引，外部必须使用双引号
        console.log(result3); //2+3=2+3=5=5
        //（2）replace函数首参是正则，2参是函数，即回调函数的情况。
        var str4 = "1234 2345 3456";
        var regex4 = /(\d)\d{2}(\d)/g;
        var ressult4 = str4.replace(regex4, function(match, $1, $2, index, input) {
            console.log([match, $1, $2, index, input]);
            //["1234", "1", "4", 0, "1234 2345 3456"]
            //["2345", "2", "5", 5, "1234 2345 3456"]
            //["3456", "3", "6", 10, "1234 2345 3456"]
        });
    </script>
</body>

</html>